class Maybe:
    """Maybe模式实现"""
    def __init__(self, value):
        self._value = value
        
    @staticmethod
    def of(value):
        return Maybe(value)
    
    def map(self, func):
        """如果有值则应用函数，否则保持None"""
        if self._value is None:
            return Maybe(None)
        try:
            return Maybe(func(self._value))
        except Exception:
            return Maybe(None)
    
    def get_or_else(self, default):
        """获取值或返回默认值"""
        return self._value if self._value is not None else default

# 使用Maybe处理可能出错的操作
def get_user(user_id):
    # 模拟数据库查询
    users = {1: {"name": "张三", "email": "zhangsan@example.com"}}
    return users.get(user_id)

def get_email(user):
    return user["email"]

def send_email(email, message):
    # 模拟发送邮件
    print(f"发送邮件到 {email}: {message}")
    return True

# 使用Maybe的函数式写法
def notify_user(user_id, message):
    result = Maybe.of(user_id)\
        .map(get_user)\
        .map(get_email)\
        .map(lambda email: send_email(email, message))
    
    return result.get_or_else("操作失败")

# 示例
# print(notify_user(1, "您的账户已被锁定，请联系管理员。"))
print(notify_user(2, "您的账户已被锁定，请联系管理员。"))